package com.qdlc.p2p.dal.mybatis.pager.core;

import com.qdlc.p2p.common.util.Page;

/**
 * 
 * 分页参数
 * 
 * @author linya 2015-06-09 19:15:45
 *
 */
public class PageParameter {

	private static final int DEFAULT_PAGE = 1;
	
	private static final int DEFAULT_PAGE_SIZE = 10;
	
	private int currentPage;

	private int pageSize;

	private int totalPage;

	private int totalCount;

	public PageParameter() {
		this(DEFAULT_PAGE, DEFAULT_PAGE_SIZE);
	}

	public PageParameter(int currentPage, int pageSize) {
		setCurrentPage(currentPage);
		setPageSize(pageSize);
	}
	
	/**
	 * 兼容以前的Page对象
	 * @return
	 */
	public Page convert2Page(){
		Page page = new Page(totalCount, currentPage, pageSize);
		return page;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		currentPage = (currentPage <= 0) ? DEFAULT_PAGE : currentPage;
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		pageSize = (pageSize <=0) ? DEFAULT_PAGE_SIZE : pageSize;
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
		if(totalCount == 0){
			totalPage = 0;
		}else{
			totalPage = totalCount % pageSize != 0 ? (totalCount / pageSize + 1) : totalCount / pageSize;
		}
	}
	
	public int getStartIndex(){
		return (currentPage - 1) * pageSize;
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer(100);
		buffer.append("{currentPage:").append(currentPage)
			.append(",pageSize:").append(pageSize)
			.append(",totalPage:").append(totalPage)
			.append(",totalCount:").append(totalCount).append("}");
		return buffer.toString();
	}

	
}
